package com.fujin.gulimall.product.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.web.bind.annotation.*;

import com.fujin.gulimall.product.entity.CategoryEntity;
import com.fujin.gulimall.product.service.CategoryService;
import com.fujin.common.utils.PageUtils;
import com.fujin.common.utils.R;

import javax.validation.Valid;


/**
 * 商品三级分类
 *
 * @author fujin
 * @email 1242257@gmail.com
 * @date 2021-12-25 10:56:06
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] catIds){
        //逻辑删除
        categoryService.removeMenuByIds(Arrays.asList(catIds));
        return R.ok();
    }

    /**
     * 查询所有分类以及子分类，并按照树形结构组装
     */
    @RequestMapping("/list/tree")
    //@RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        List<CategoryEntity> entities = categoryService.listWithTree();

        return R.ok().put("data", entities);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
    //@RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);

        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@Valid @RequestBody CategoryEntity category){
		categoryService.save(category);

        return R.ok();
    }

    /**
     * 修改三级分类、对应关联表也需要修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@Valid @RequestBody CategoryEntity category){
		categoryService.updateDetail(category);

        return R.ok();
    }

    /**
     * 批量修改
     */
    @RequestMapping("/update/sort")
    //@RequiresPermissions("product:category:update")
    public R updateAll(@RequestBody CategoryEntity[] category){
        categoryService.updateBatchById(Arrays.asList(category));
//        categoryService.updateById(category);

        return R.ok();
    }

    /**
     * 删除
     */
//    @RequestMapping("/delete")
//    //@RequiresPermissions("product:category:delete")
//    public R delete(@RequestBody Long[] catIds){
//		categoryService.removeByIds(Arrays.asList(catIds));
//
//        return R.ok();
//    }

}
